﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using xIx.Data;

public partial class Site_MyAccountMan : xIx.Web.UI.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitView();
        }
    }


    public DataTable TempUser
    {
        get { return Page.Cache["TempUser"] as DataTable; }
        set { Page.Cache["TempUser"] = value; }
    }

    public string Uid
    {
        get
        {
            object v = ViewState["Uid"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["Uid"] = value;
        }
    }

    private void InitView()
    {

        if (User!=null && User.Identity.IsAuthenticated)
        {
            Uid = User.AccountInfo.ID;
            IsNew = false;
        }
        else
        {
            Uid = xIx.Data.DbHelper.StaticGenUid().ToString();
            IsNew = true;
        }
        DataTable dt = ReloadUsersTab();
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            dt.Rows[0]["ID"] = Uid;
            dt.Rows[0]["Province"] = "";
        }

        TempUser = dt;

        DetailsView1.DataSource = dt;
        DetailsView1.DataBind();
    }
    public bool IsNew
    {
        get
        {
            object v = ViewState["IsNew"];
            if (null != v)
            {
                return (bool)v;
            }
            else
            {
                return default(bool);
            }
        }
        set
        {
            ViewState["IsNew"] = value;
        }
    }

    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        string pwd = (DetailsView1.Rows[3].Cells[1].FindControl("TextBox1") as TextBox).Text;
        if (pwd != (DetailsView1.Rows[3].Cells[1].FindControl("TextBox2") as TextBox).Text)
        {
            e.Cancel = true;
            Alert("两次密码输入不一致，请重试。");
            return;
        }
        pwd = pwd.Trim();

        DataRow dr = TempUser.Rows[0];
        SortedList<string, object> sl = new SortedList<string, object>();
        if (IsNew)
        {
            sl.Add("ID", dr["ID"]);
            sl.Add("isactive", false);
            sl.Add("RoleID", "Member"); sl.Add("GroupID","1");
            sl.Add("regreqtime", DateTime.Now);
            sl.Add("isregaudit", false);

            if (pwd.Trim().Length < 3)
            {
                e.Cancel = true;
                Alert("注册时必须指定密码。密码必须三位以上");
                return;
            }
            else
            {
                sl.Add("pwd", pwd);
            }
        }
        else
        {
            if (pwd.Trim() != string.Empty && pwd.Trim().Length < 3)
            {
                e.Cancel = true;
                Alert("密码必须三位以上");
                return;
            }
            else if(pwd.Trim()!=string.Empty)
            {
                sl.Add("pwd", pwd.Trim());
            }
        }




        TextBox tb = null;
        DropDownList ddl = null;

        int[] ria = new int[] { 1, 2, 5, 6, 7, 8, 9 };
        string[] rna = "Name,Title,addr,email,telephone,cellphone,descript".Split(',');

        for (int i = 0; i < ria.Length; i++)
        {
            tb = (DetailsView1.Rows[ria[i]].Cells[1].Controls[0] as TextBox);
            if (tb.Text != dr[rna[i]].ToString()) sl.Add(rna[i], tb.Text);
        }
        if (rna[0].Trim() == string.Empty || rna[1].Trim() == string.Empty)
        {
            Alert("请填写所有必填项");
            e.Cancel = true;
            return;
        }
        tb = (DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox);
        if (tb.Text != dr["Province"].ToString()) sl.Add("Province", tb.Text);

        if (sl.Count > 0)
        {
            using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
            {
                List<string> fl = new List<string>(); fl.AddRange(sl.Keys);
                List<object> vl = new List<object>(); vl.AddRange(sl.Values);

                int r = -1;
                if (IsNew)
                {
                    r = dh.Table["Sys_Users"].Fields(fl.ToArray()).InsertWith(vl.ToArray()).Exec();
                }
                else
                {
                    r = dh.Table["Sys_Users"].Fields(fl.ToArray()).UpdateWith(vl.ToArray()).Where("ID=@id").Exec(dr["ID"]);
                }
                Alert("操作" + (r == 1 ? "成功" : "失败。"));
            }
        }
        TempUser = ReloadUsersTab();
        DetailsView1.DataSource = TempUser;
        DetailsView1.DataBind();

    }

    private DataTable ReloadUsersTab()
    {
        DataTable dt = null;
        using (xIx.Data.DbHelper dh = new DbHelper())
        {
            //用.Fields控制只要内容字段。系统字段不要。
            dt = dh.Table["Sys_Users"].Where("ID=@id").Select("id", Uid);
        }
        return dt;
    }
}
